/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfejs;

import java.awt.event.*;
import java.awt.Color;
import java.awt.event.ActionEvent;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import mechanika.*;

/**
 *
 * @author Tomek
 */
public class GamePlay extends javax.swing.JPanel implements ActionListener {

    //public javax.swing.JPanel[][] panel = new javax.swing.JPanel[8][4];
    public javax.swing.JButton[][] buttony = new javax.swing.JButton[8][4]; //deklaracja tablicy na plansze
    public Color kolorPlanszy = Color.black; //kolor planszy - ciemne pola
    public static boolean ruchBialego = true; //czyj ruch
    javax.swing.ImageIcon pionekJasny = new javax.swing.ImageIcon(getClass().getResource("/import/graph/pionek_jasny.png"));
    javax.swing.ImageIcon pionekCiemny = new javax.swing.ImageIcon(getClass().getResource("/import/graph/pionek_ciemny.png"));
    javax.swing.ImageIcon pionekJasnyDama = new javax.swing.ImageIcon(getClass().getResource("/import/graph/pionek_jasny_dama.png"));
    javax.swing.ImageIcon pionekCiemnyDama = new javax.swing.ImageIcon(getClass().getResource("/import/graph/pionek_ciemny_dama.png"));
    public static int oldXtmp = -1, oldYtmp = -1; //definiowanie zmiennych pomocnyczych
    DefaultListModel listModel = new DefaultListModel(); 
    private PoleGry poleGry;

    public GamePlay() {
        initComponents();

        poleGry = new PoleGry();


        this.setBounds(0, 0, 480, 640);
        this.setVisible(false);
        Game.panelGlowny.add(this);
        dajPanela();
        inicjujPionki();
        // inicjujPionkiCzarne();
        czyjRuch();
        JList list = new JList(listModel);  
    }

    //dodaje do kontenera plansze jako pojedyncze buttony : tablica dwuwymiarowa buttony[i][j] 7x3
    public void dajPanela() {
        int x = 0;
        int y = 0;
        int width = 60;
        int height = 60;

        for (int i = 0; i < 8; i++) {
            x = 0;
            if (i % 2 > 0) {
                x = 60;
            }

            for (int j = 0; j < 4; j++) {
                /*
                 panel[i][j] = new javax.swing.JPanel();
                 panel[i][j].setBounds(x, y, width, height);
                 panel[i][j].setBackground(Color.black);
                 panel[i][j].setVisible(true);
                 jPanel1.add(panel[i][j]);
                 */
                buttony[i][j] = new javax.swing.JButton();
                buttony[i][j].setBounds(x, y, width, height);
                buttony[i][j].setBackground(Color.black);
                //buttony[i][j].setText(Integer.toString(i)+":"+Integer.toString(j));
                buttony[i][j].setName(Integer.toString(i) + ":" + Integer.toString(j));
                buttony[i][j].addActionListener(this);
                buttony[i][j].setVisible(true);
                jPanel1.add(buttony[i][j]);

                x = x + 120;
            }
            y = y + 60;
        }
        jPanel1.validate();
        jPanel1.repaint();
    }

    //wyswietla czyj ruch
    public boolean czyjRuch() {
        if (ruchBialego) {
            updatePodgladZdarzen("Ruch JASNEGO");
            return true;
        } else {
            updatePodgladZdarzen("Ruch CIEMNEGO");
            jList1.setModel(listModel);
            return false;
        }
    }
    
    //dodawanie i wyswietlanie komuniaktow
    public void updatePodgladZdarzen(String komunikat){
        listModel.addElement(komunikat);
        jList1.setModel(listModel);
            
        int lastIndex = jList1.getModel().getSize() - 1;
        if (lastIndex >= 0) {
        jList1.ensureIndexIsVisible(lastIndex);
        }
    }
    
    //obsluga zdarzen buttonow planszy
    @Override
    public void actionPerformed(ActionEvent event) {
        Object src = event.getSource();

            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 4; j++) {
                    //jezeli zrodlo obiektu z eventu jest rowne obiektowi tablicy to wykonuje na nim dzialania
                    if (src.equals(buttony[i][j])) {
                        System.out.println("klik! " + i + ":" + j + ";");
                        //jezeli stare wspolrzedne maja konkretna wartosc to wykonuj podmiane
                        if ((oldXtmp != -1) && (oldYtmp != -1)) {
                            //jLabel3.setText(Integer.toString(oldXtmp));
                            //jLabel4.setText(Integer.toString(oldYtmp));
                            //jLabel5.setText(Integer.toString(i));
                            //jLabel6.setText(Integer.toString(j));

                            //jezeli na polu nie ma zadnego pionka to wstawiamy
                            //a jak jest to komunikat
                            if (buttony[i][j].getIcon() == null) {
                                buttony[oldXtmp][oldYtmp].setBackground(kolorPlanszy);


                                this.wstawPionek(oldXtmp, oldYtmp, i, j, ruchBialego, poleGry.getPionek(oldYtmp,oldXtmp).czyDamka());
                                ruchBialego = (!ruchBialego);
                                oldXtmp = -1;
                                oldYtmp = -1;
                                this.czyjRuch();
                            } else if (buttony[i][j].getIcon() == (this.czyjRuch() ? pionekJasny : pionekCiemny)) {
                                updatePodgladZdarzen("pole zajete!");
                                this.czyjRuch();
                            } else {
                                buttony[oldXtmp][oldYtmp].setBackground(kolorPlanszy);
                                poleGry.wykonajZbicie(oldYtmp, oldXtmp, j, i);                                
                                ruchBialego = (!ruchBialego);
                                oldXtmp = -1;
                                oldYtmp = -1;
                                inicjujPionki();
                                this.czyjRuch();
                            }
                        } else {
                            //jezeli wybierzemy button bez pionka to komunikat
                            if (buttony[i][j].getIcon() == null) {
                                updatePodgladZdarzen("puste pole!");
                                this.czyjRuch();
                            }

                            //jezeli wybierzemy button ciemnego, a jest ruch ciemnego to wykonaj, inaczej nic nie wybierze
                            if (buttony[i][j].getIcon() == pionekCiemny && !ruchBialego) {
                                oldXtmp = i;
                                oldYtmp = j;
                                buttony[i][j].setBackground(Color.yellow);
                            }else

                            //jezeli wybierzemy button jasnego, a jest ruch jasnego to wykonaj, inaczej nic nie wybierze
                            if (buttony[i][j].getIcon() == pionekJasny && ruchBialego) {
                                oldXtmp = i;
                                oldYtmp = j;
                                buttony[i][j].setBackground(Color.yellow);
                            }else
                            if (buttony[i][j].getIcon() == pionekJasnyDama && ruchBialego) {
                                oldXtmp = i;
                                oldYtmp = j;
                                buttony[i][j].setBackground(Color.yellow);
                            }else
                            if (buttony[i][j].getIcon() == pionekCiemnyDama && !ruchBialego) {
                                oldXtmp = i;
                                oldYtmp = j;
                                buttony[i][j].setBackground(Color.yellow);
                            }

                        }
                    }
                }
            }
        
    }

    //metoda wstawiania pionka - przy inicjacji tablicy
    public void wstawPionek(int x, int y, boolean czyBiale) {
        Color kolor = null;
        if (czyBiale == true) {
            kolor = Color.white;
        } else {
            kolor = Color.black;
        }
        //buttony[x][y].setBackground(kolor);
        buttony[x][y].setIcon(pionekJasny);
        jPanel1.validate();
        jPanel1.repaint();
    }
    
    //metoda ruszania sie pionkiem: stare, nowe dane i kolor
    
    //metoda ruszania sie pionkiem: stare, nowe dane, kolor i rodzaj pionka
    public void wstawPionek(int oldX, int oldY, int newX, int newY, boolean czyBiale, boolean czyDamka) {
        Color kolor = null;
        
        poleGry.wykonajRuch(oldY, oldX, newY, newX);
        buttony[oldX][oldY].setIcon(null);
        if(!poleGry.getPionek(newY, newX).czyDamka())
            buttony[newX][newY].setIcon(czyBiale ? pionekJasny : pionekCiemny);
        else
            buttony[newX][newY].setIcon(czyBiale ? pionekJasnyDama : pionekCiemnyDama);
 
    }

    //moje metody (jak nie mialem tablisy,listy pionkow - tutaj mozesz uzyc swojej funkcji z wykorzystaniem metod wstawPionek)
    public void inicjujPionki() {
        boolean czyBiale;
        int index;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 8; j++) {
                index = poleGry.pole[i][j];
                if (index >= 0) {
                    czyBiale = PoleGry.pionki.get(index).czyBiale() == 1 ? true : false;
                    //buttony[i][j].setBackground(Color.red);
                    wstawPionek(j, i, czyBiale);
                    buttony[j][i].setIcon(czyBiale ? pionekJasny : pionekCiemny);
                    buttony[j][i].setVisible(true);
                } else {
                    buttony[j][i].setIcon(null);
                    buttony[j][i].setVisible(true);
                }
            }
        }
        jPanel1.validate();
        jPanel1.repaint();
    }

    //moje metody (jak nie mialem tablisy,listy pionkow - tutaj mozesz uzyc swojej funkcji z wykorzystaniem metod wstawPionek)
  /*  public void inicjujPionkiCzarne(){
     for(int i=7; i>5; i--){
     for(int j=0; j<4; j++){
     //buttony[i][j].setBackground(Color.green);
     buttony[i][j].setIcon(pionekCiemny);
     buttony[i][j].setVisible(true);   
     }
     }
     jPanel1.validate();
     jPanel1.repaint();
     }*/
    //metoda do zaladowania na ekranu z plansza
    public void LoadScreenPlay() {
        this.setVisible(true);
        Game.panelGlowny.validate();
        Game.panelGlowny.repaint();
    }

    //metoda do przestania wyswietlania na ekranie planszy
    public void UnLoadScreenPlay() {
        this.setVisible(false);
        Game.panelGlowny.validate();
        Game.panelGlowny.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        timerLbl = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        GraczJasnyNameLbl = new javax.swing.JLabel();
        GraczJasnyPktLbl = new javax.swing.JLabel();
        GraczCiemnyNameLbl = new javax.swing.JLabel();
        GraczCiemnyPktLbl = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(480, 640));
        setPreferredSize(new java.awt.Dimension(480, 640));
        setLayout(null);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setMaximumSize(new java.awt.Dimension(480, 480));
        jPanel1.setMinimumSize(new java.awt.Dimension(480, 480));
        jPanel1.setPreferredSize(new java.awt.Dimension(480, 480));
        jPanel1.setLayout(null);
        add(jPanel1);
        jPanel1.setBounds(0, 0, 485, 480);

        jButton1.setText("Zakoncze gre");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1);
        jButton1.setBounds(340, 590, 140, 50);

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setAlignmentX(0.0F);
        jList1.setAlignmentY(0.0F);
        jScrollPane1.setViewportView(jList1);
        jList1.getAccessibleContext().setAccessibleParent(this);

        add(jScrollPane1);
        jScrollPane1.setBounds(0, 540, 340, 100);

        timerLbl.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        timerLbl.setText("00:02:03");
        timerLbl.setToolTipText("");
        timerLbl.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jLabel4.setText("Czas gry:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(timerLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(3, 3, 3)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(timerLbl, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        timerLbl.getAccessibleContext().setAccessibleName("timerLbl");

        add(jPanel2);
        jPanel2.setBounds(340, 540, 140, 50);

        GraczJasnyNameLbl.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        GraczJasnyNameLbl.setForeground(new java.awt.Color(0, 102, 255));
        GraczJasnyNameLbl.setText("ROMAN");

        GraczJasnyPktLbl.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        GraczJasnyPktLbl.setText("5");

        GraczCiemnyNameLbl.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        GraczCiemnyNameLbl.setForeground(new java.awt.Color(255, 0, 0));
        GraczCiemnyNameLbl.setText("SYLWIA");

        GraczCiemnyPktLbl.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        GraczCiemnyPktLbl.setText("20");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(3, 3, 3)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(GraczCiemnyNameLbl)
                        .addGap(78, 78, 78)
                        .addComponent(GraczCiemnyPktLbl))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(GraczJasnyNameLbl)
                        .addGap(88, 88, 88)
                        .addComponent(GraczJasnyPktLbl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap(304, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(GraczJasnyPktLbl)
                        .addGap(4, 4, 4))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(GraczJasnyNameLbl)
                        .addGap(2, 2, 2)))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(GraczCiemnyNameLbl)
                    .addComponent(GraczCiemnyPktLbl))
                .addContainerGap())
        );

        GraczJasnyNameLbl.getAccessibleContext().setAccessibleName("GraczJasnyNameLbl");
        GraczJasnyPktLbl.getAccessibleContext().setAccessibleName("GraczJasnyPktLbl");
        GraczCiemnyPktLbl.getAccessibleContext().setAccessibleName("GraczCiemnyPktLbl");

        add(jPanel3);
        jPanel3.setBounds(0, 480, 480, 60);
    }// </editor-fold>//GEN-END:initComponents

    //przysk do powrotu na ekran glowny - obiek sie nie kasuje tylko przestaje byc wyswietlany, w koncowej wersji bedzie on usuwany
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Game.gpObj.UnLoadScreenPlay();
        Game.gwObj.LoadScreenWelcome();
        //Game.gpObj = null;
        
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel GraczCiemnyNameLbl;
    private javax.swing.JLabel GraczCiemnyPktLbl;
    private javax.swing.JLabel GraczJasnyNameLbl;
    private javax.swing.JLabel GraczJasnyPktLbl;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jList1;
    private static javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel timerLbl;
    // End of variables declaration//GEN-END:variables
}
